package com.zcw.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AddMarkContent {

	private static final int TASK_HEIGHT = 50;

	public static void main(String[] args) throws IOException {
		addMarkContent();
	}

	private static void addMarkContent() throws IOException {

		String srcImgPath = "C:/Users/Administrator/Pictures/bing/20200418_100158.jpg";
		String waterMarkContent = "优胜美地国家公园中的春季瀑布 (© elvistudio/Shutterstock)";

		String descFilePath = "C:/Users/Administrator/Pictures/bing/20200418_100158_1.jpg";
		String fileName2 = "C:/Users/Administrator/Pictures/bing/20200418_100158_2.jpg";

		// 读取源文件
		BufferedImage srcImg = ImageIO.read(new File(srcImgPath));
		int imgWidth = srcImg.getWidth();
		int imgHeight = srcImg.getHeight();

		int fontSize = 26;
		Font font = new Font("微软雅黑", Font.PLAIN, fontSize);
		int textLength = getTextLength(waterMarkContent);

		int shadowWidth = textLength * fontSize;
		int shadowHeight = fontSize * 3 / 2;

		int startX = imgWidth - shadowWidth;
		int startY = imgHeight - shadowHeight - TASK_HEIGHT;
		int strX = startX + fontSize / 2;
		int strY = startY + fontSize + fontSize / 6;

		shadowHeight += TASK_HEIGHT;

		createDropShadow(srcImg, descFilePath, startX, startY, shadowWidth, shadowHeight);
		createString(descFilePath, waterMarkContent, fileName2, font, strX, strY);
	}

	private static void createDropShadow(BufferedImage image, String descFilePath, int startX, int startY,
			int shadowWidth, int shadowHeight) throws IOException {

		BufferedImage shadow = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);

		Graphics2D g = shadow.createGraphics();
		g.drawImage(image, 0, 0, null);

		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.6f));
		g.setColor(Color.gray);
		g.fillRoundRect(startX, startY, shadowWidth, shadowHeight, 50, 50);

		g.dispose();
		// shadow = getGaussianBlurFilter(size, true).filter(shadow, null);
		ImageIO.write(shadow, "PNG", new File(descFilePath));
	}

	private static RenderedImage createString(String srcImgPath, String waterMarkContent, String descFilePath,
			Font font, int strX, int strY) throws IOException {

		// 读取源文件
		BufferedImage image = ImageIO.read(new File(srcImgPath));

		BufferedImage shadow = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = shadow.createGraphics();
		g.drawImage(image, 0, 0, null);
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

		g.setColor(Color.white);
		g.setFont(font);

		g.drawString(waterMarkContent, strX, strY);

		g.dispose();
		// shadow = getGaussianBlurFilter(size, true).filter(shadow, null);

		ImageIO.write(shadow, "PNG", new File(descFilePath));
		return shadow;
	}


	/**
	 * 获取文本长度。汉字为1:1，英文和数字为2:1
	 */
	private static int getTextLength(String text) {
		int length = text.length();
		for (int i = 0; i < text.length(); i++) {
			String s = String.valueOf(text.charAt(i));
			if (s.getBytes().length > 1) {
				length++;
			}
		}
		length = length % 2 == 0 ? length / 2 : length / 2 + 1;
		return length;
	}
}
